package employee.web.beans;


import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import common.MessageCallback;
import customer.ejb.eaos.ExcursionManager;
import customer.ejb.eaos.HotelList;
import customer.ejb.eaos.MeanOfTransportationManager;
import dtos.ExcursionDTO;
import dtos.ExcursionPersTPDTO;
import dtos.ExcursionPredTPDTO;
import dtos.HotelDTO;
import dtos.MeanOfTransportationDTO;
import dtos.PredefinedTPDTO;
import dtos.UserDTO;
import employee.ejb.management.PackageMgr;




@ManagedBean(name="packageBean")
@ViewScoped
public class PackageBean {
	
	@EJB
	private PackageMgr packMgr;
	@EJB
	private MeanOfTransportationManager meanMgr;
	@EJB
	private HotelList hotelMgr;
	@EJB
	private ExcursionManager excMgr;

	private PredefinedTPDTO predTP;
	private PredefinedTPDTO selectedTravel;
	private DataModel<PredefinedTPDTO> _predTPModel;
	private DataModel<MeanOfTransportationDTO> returns;
	private DataModel<HotelDTO> hotels;
	private DataModel<ExcursionDTO> excursions;
	
	private List<ExcursionPredTPDTO> listJoin = new LinkedList<ExcursionPredTPDTO>();
	private DataModel<ExcursionDTO> excursionsInPredTP;


	@PostConstruct
	public void init(){
		refreshPredTPList();
		
		List<ExcursionDTO> excursionList = new LinkedList<ExcursionDTO>();
		excursionsInPredTP = new ListDataModel<>(excursionList);
	}
	
	public void refreshPredTPList(){
		predTP = new PredefinedTPDTO();
		List<PredefinedTPDTO> tps = new LinkedList<>();
		for(PredefinedTPDTO tp : packMgr.getPredTPList())
			tps.add(tp);
		_predTPModel = new ListDataModel<>(tps);
	}
	
	public void refreshReturnList(){
		String arrivalPlace = predTP.getMeanOfTransportation1().getTrip().getArrivalPlace();
		String departurePlace = predTP.getMeanOfTransportation1().getTrip().getDeparturePlace();
		List<MeanOfTransportationDTO> t = new LinkedList<>();
		for(MeanOfTransportationDTO trans : meanMgr.getTransportations(arrivalPlace, departurePlace))
			t.add(trans);
		returns = new ListDataModel<>(t);
	}
	
	public void refreshHotelList(){
		String place = predTP.getMeanOfTransportation1().getTrip().getArrivalPlace();
		List<HotelDTO> h = new LinkedList<>();
		for(HotelDTO hotel : hotelMgr.getHotelList(place)){
			h.add(hotel);
		}
		
		hotels = new ListDataModel<>(h);
		
	}
	
	public void refreshExcursionList(){
		String place = predTP.getMeanOfTransportation1().getTrip().getArrivalPlace();
		List<ExcursionDTO> e = new LinkedList<>();
		for(ExcursionDTO exc : excMgr.getExcursionList(place)){
			e.add(exc);
		}
		
		for (ExcursionDTO exc : excursionsInPredTP){
			for (ExcursionDTO e2 : e){
				if (e2.getExcursionID() == exc.getExcursionID()){
					e.remove(e2);
				}
			}
		}
		excursions = new ListDataModel<>(e);
	}

	public PredefinedTPDTO getPredTP() {
		return predTP;
	}

	public void setPredTP(PredefinedTPDTO predTP) {
		this.predTP = predTP;
	}
	
	public String createPredTP(UserDTO employee){
		this.predTP.setUser(employee);
		packMgr.savePredTP(predTP, new MessageCallback(FacesContext.getCurrentInstance()));
		for (ExcursionPredTPDTO e : listJoin){
			packMgr.saveExcursionPredTP(e);
		}
		
		return "employeeHome?faces-redirect=true";
	}
	
	public void setTPHotel(HotelDTO hotel){
		predTP.setHotelBean(hotel);
	}
	
	public void setTPGo(MeanOfTransportationDTO mean){
		predTP.setMeanOfTransportation1(mean);
		refreshReturnList();
		refreshHotelList();
		refreshExcursionList();
		
	}
	
	public void setTPReturn(MeanOfTransportationDTO mean){
		predTP.setMeanOfTransportation2(mean);
	}
	
	public void addTPExcursion(ExcursionDTO e){
		
		ExcursionPredTPDTO exc = new ExcursionPredTPDTO();
		exc.setExcursion(e);
		exc.setPredefinedTp(predTP);
		listJoin.add(exc);
		
		List<ExcursionDTO> excursionList = new LinkedList<ExcursionDTO>();
		for (ExcursionDTO edto : excursionsInPredTP){
			excursionList.add(edto);
		}
		excursionList.add(e);
		excursionsInPredTP = new ListDataModel<>(excursionList);
		
		refreshExcursionList();
		
	}
	
	public void removeTPExcursion(ExcursionDTO excursion){
		
		for (ExcursionPredTPDTO exc : listJoin){
			if (exc.getExcursion().getExcursionID() == excursion.getExcursionID()){
				listJoin.remove(exc);
			}
		}
		
		List<ExcursionDTO> e = new LinkedList<>();
		for(ExcursionDTO exc : excursionsInPredTP){
			e.add(exc);
		}
		for (ExcursionDTO exc : e){
			if (exc.getExcursionID() == excursion.getExcursionID()){
				e.remove(excursion);
			}
		}

		e.add(excursion);
		excursionsInPredTP = new ListDataModel<>(e);

		refreshExcursionList();
	}
	
	public void deletePredTP(PredefinedTPDTO predTP){
		packMgr.removePredTP(predTP.getTravelID());
		predTP = new PredefinedTPDTO();
		
		refreshPredTPList();
		
		FacesMessage msg = new FacesMessage("Travel package deleted");  
		  
        FacesContext.getCurrentInstance().addMessage(null, msg);
        
		
	}
	
	public DataModel<PredefinedTPDTO> getPredTPModel(){
		return _predTPModel;
	}
	
	public DataModel<MeanOfTransportationDTO> getReturns(){
		return returns;
	}
	
	public DataModel<HotelDTO> getHotels(){
		return hotels;
	}
	
	public DataModel<ExcursionDTO> getExcursions(){
		return excursions;
	}
	
	public DataModel<ExcursionDTO> getExcList(){
		return excursionsInPredTP;
	}

	public PredefinedTPDTO getSelectedTravel() {
		return selectedTravel;
	}

	public void setSelectedTravel(PredefinedTPDTO selectedTravel) {
		this.selectedTravel = selectedTravel;
	}
}
